//
//  ViewController.swift
//  C011_test_UIScrollView
//
//  Created by yao_yu on 15/6/2.
//  Copyright (c) 2015年 yao_yu. All rights reserved.
//

import UIKit

class CustomScrollView: UIView {
    var contentSize:CGSize = CGSizeMake(0, 0)
    var scrollVertical = true
    var scrollHorizontal = true

    var startBounds = CGRectZero
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInitForCustomScrollView()
    }
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInitForCustomScrollView()
    }
    
    func commonInitForCustomScrollView() {
        scrollHorizontal = true
        scrollVertical = true
        
        let gestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
        self.addGestureRecognizer(gestureRecognizer)
    }
    

    
    func handlePanGesture(gestureRecognizer: UIPanGestureRecognizer) {
        let translation = gestureRecognizer.translationInView(self)
        
        let newBoundsOriginX = bounds.origin.x - translation.x
        let minBoundsOriginX:CGFloat = 0.0
        let maxBoundsOriginX = self.contentSize.width - bounds.size.width
        bounds.origin.x = fmax(minBoundsOriginX, fmin(newBoundsOriginX, maxBoundsOriginX))
        
        let newBoundsOriginY = bounds.origin.y - translation.y
        let minBoundsOriginY:CGFloat = 0.0
        let maxBoundsOriginY = self.contentSize.height - bounds.size.height
        bounds.origin.y = fmax(minBoundsOriginY, fmin(newBoundsOriginY, maxBoundsOriginY))
        
        self.bounds = bounds
        gestureRecognizer.setTranslation(CGPointZero, inView:self)
    }
}


class ViewController: UIViewController {
    
    var mainView: CustomScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()

        mainView = CustomScrollView(frame: view.frame)
        view.addSubview(mainView)
        
        addTestViews()
        
        var bounds = mainView.bounds;
        bounds.origin = CGPointMake(0, 100)
        mainView.bounds = bounds;
    }
    
    func addTestViews() {
        let redView:UIView = UIView(frame: CGRectMake(20, 20, 100, 100))
        redView.backgroundColor = UIColor(red: 0.815, green: 0.007, blue: 0.105, alpha: 1)

        let greenView:UIView = UIView(frame: CGRectMake(150, 160, 150, 200))
        greenView.backgroundColor = UIColor.greenColor()

        let blueView:UIView = UIView(frame: CGRectMake(40, 400, 200, 150))
        blueView.backgroundColor = UIColor.blueColor()

        let yellowView:UIView = UIView(frame: CGRectMake(100, 600, 180, 150))
        yellowView.backgroundColor = UIColor.yellowColor()
        
        mainView.addSubview(redView)
        mainView.addSubview(greenView)
        mainView.addSubview(blueView)
        mainView.addSubview(yellowView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

